package com.glela.test;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.log4j.LogManager;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.glela.api.model.AlipayAppKeyInfo;
import com.glela.api.model.PayAppKeyInfo;
import com.glela.api.pay.alipay.component.Alipay;
import com.glela.api.pay.wechat.component.WeChat;
import com.glela.api.pay.wechat.component.impl.WeChat0Impl;
import com.glela.api.pay.wechat.component.impl.WeChatImpl;
import com.glela.common.constant.PayTypeEnum;
import com.glela.order.mapper.OrderPayMapper;
import com.glela.order.model.OrderObject;
import com.glela.order.model.OrderPay;
import com.glela.order.service.OrderPayService;
import com.glela.order.service.OrderService;

public class ValidationCannelOrder implements Serializable,Callable<List<Map<String, Object>>> {
//	private static Logger logger = LoggerFactory.getLogger(ValidationCannelOrder.class);
	private static org.apache.log4j.Logger logger=LogManager.getLoggerRepository().getLogger("ValidationCannelOrder");
	private static final long serialVersionUID = -6384339684247960294L;
	private List<OrderObject> PaylistOBJ;
	private OrderPayMapper orderPayMapper;
	private OrderPayService orderPayService;
	private List<WeChat> weChatList;
	private OrderService orderService2;
	private Alipay alipay;

	public ValidationCannelOrder(List<OrderObject> paylistOBJ,
			OrderPayMapper orderPayMapper, OrderPayService orderPayService,List<WeChat> weChatList,OrderService orderService2,Alipay alipay) {
		super();
		PaylistOBJ = paylistOBJ;
		this.orderPayMapper = orderPayMapper;
		this.orderPayService = orderPayService;
		this.weChatList = weChatList;
		this.orderService2 = orderService2;
		this.alipay = alipay;
	}
	public List<Map<String, Object>> call() {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		Map<String, Object> temp = null;
		int [] YeePayType={20,21,22,23,24};//易宝支付
		int [] WechaPayType={1,3};//微信支付
		int [] AliPayType={2};//支付宝支付
		// 验证未支付
		int i=0;
		int j=PaylistOBJ.size();
		for (OrderObject OrderObj : PaylistOBJ) {
			List<OrderPay> orderPayList = orderPayMapper
					.selectAllByOrderSn(OrderObj.getOrderSn());
			logger.error((i++)+"  "+j+" "+OrderObj.getOrderSn());
			for (OrderPay orderPay : orderPayList) {
				try {
					String status =null;
					if(ArrayUtils.contains(YeePayType,orderPay.getPayType())){//易宝支付
						status =orderPayService
								.queryYeePayOrderStatus(orderPay.getPaySn());
						if ("SUCCESS".equals(status.toUpperCase())) {// 支付成功
							Map<String, Object> map = new HashMap<String, Object>();
							map.put("1", orderPay.getOrderSn());
							map.put("2","该订单支付成功  app显示支付未成功 " + orderPay.getPaySn()+ " " + orderPay.getOrderSn());
							result.add(map);
						}else if("RESTART".equals(status.toUpperCase())){//请求超时 重新 请求直到成功为止
							status = orderPayService.queryYeePayOrderStatus(orderPay.getPaySn());
							while("RESTART".equals(status.toUpperCase())){
								status = orderPayService.queryYeePayOrderStatus(orderPay.getPaySn());
								if(!"RESTART".equals(status.toUpperCase())){
									break;
								}
							}
							Map<String, Object> map = new HashMap<String, Object>();
							map.put("1", orderPay.getOrderSn());
							map.put("2","该订单支付成功  app显示支付未成功 " + orderPay.getPaySn()+ " " + orderPay.getOrderSn());
							result.add(map);
						}
						
					}
					if(ArrayUtils.contains(WechaPayType,orderPay.getPayType())){//微信支付
						PayAppKeyInfo payAppKeyInfo =orderService2.getPayAppKeyInfo(orderPay.getPayType());
						Map<String, Object> outmap =null;
						while(outmap==null){
							try {
								outmap=getWeChat(orderPay.getPayType()).weChaPayOrderQuery(
										payAppKeyInfo,orderPay.getPaySn());
								if(outmap.get("err_code_des")!=null&&
										"order not exist".equals(outmap.get("err_code_des").toString())){//订单不存在
									status="error";
									break;
								}
									status =outmap.get("trade_state").toString();
							} catch (Exception e) {//防止请求失败
								logger.error("微信查询请求失败 重新发送:"+e.getMessage()+" "+orderPay.getOrderSn());
									outmap=null;
							}
						}
						
						if ("SUCCESS".equals(status.toUpperCase())) {// 微信支付成功
							Map<String, Object> map = new HashMap<String, Object>();
							map.put("1", orderPay.getOrderSn());
							map.put("2","该订单支付成功  app显示支付未成功 " + orderPay.getPaySn()+ " " + orderPay.getOrderSn());
							result.add(map);
						} 
					}
					if(ArrayUtils.contains(AliPayType,orderPay.getPayType())){//支付宝支付
						AlipayAppKeyInfo alipayAppKeyInfo=orderService2.getAlipayAppKeyInfo();
						AlipayTradeQueryResponse responese=null;
						while(responese==null){
							try {
								 responese=alipay.alipayTradeQuery(alipayAppKeyInfo,orderPay.getPaySn());
							} catch (Exception e) {//防止请求失败
								logger.error("支付宝请求失败 开始重新发送:"+e.getMessage()+" "+orderPay.getOrderSn());
								responese=null;
							}
						}
						if ("TRADE_SUCCESS".equals(responese.getTradeStatus())) {// 微信支付成功
							Map<String, Object> map = new HashMap<String, Object>();
							map.put("1", orderPay.getOrderSn());
							map.put("2","该订单支付成功  app显示支付未成功 " + orderPay.getPaySn()+ " " + orderPay.getOrderSn());
							result.add(map);
						} 
					}
				} catch (Exception e) {
					logger.error("PaylistOBJ：" + e.getMessage());
					temp=new HashMap<String, Object>();
					temp.put("1", OrderObj.getOrderSn());
					temp.put("2", "未支付单验证失败 请重新验证" + e.getMessage());
				}
			}
		}
		return result;
	}
	private WeChat getWeChat(int payType) {
		WeChat theWeChat = null;
		if (payType == PayTypeEnum.T1.getCode()) {
			for (WeChat weChat : weChatList) {
				if (weChat instanceof WeChat0Impl) {
					theWeChat = weChat;
				}
			}
		} else if (payType == PayTypeEnum.T3.getCode()) {
			for (WeChat weChat : weChatList) {
				if (weChat instanceof WeChatImpl) {
					theWeChat = weChat;
				}
			}
		} else {
			theWeChat = weChatList.get(0);
		}
		return theWeChat;
	}
}
